//
//  CHjzCameraViewController.m
//  proofSdkProj
//
//  Created by cyptt on 2022/3/10.
//

#import "CHjzCameraViewController.h"
#import <AVFoundation/AVFoundation.h>
#import "CHCameraUIView.h"
#import "CHCameraUIManage.h"
#import "CHRecordData.h"
#import "CHRecordFile.h"
#import "CHRecordPhoto.h"
@interface CHjzCameraViewController ()

//@property(nonatomic,strong) CHCameraPreView * preview ;
@property(nonatomic,strong)CHCameraUIManage * uiManage;

@property(nonatomic,assign)CH_RECORD_PHOTO_TYPE  recordType;
@property(nonatomic,strong)CHRecordData * cameraManage;
@property(nonatomic,strong)CHRecordFile * recordFile;

@property(nonatomic,strong)CHRecordPhoto * recordPhoto;
@property(nonatomic,strong)AVCaptureSession * session;
@property(nonatomic,strong)AVCaptureVideoPreviewLayer * previewLayer;   //预览view

@property(nonatomic,strong)void (^successBlock)(id responseObj);
@property(nonatomic,strong)void (^failBlock)(NSError * error);

@property(nonatomic,assign)BOOL isSaveAblum; //是否保存相册
@end

@implementation CHjzCameraViewController
- (instancetype)initWithRecordPhotoType:(CH_RECORD_PHOTO_TYPE)type isSaveAblum:(BOOL)isSaveAblum SuccessBlock:(void (^ _Nonnull)(id responseObj))successBlock failBlock:(void (^ _Nonnull)(NSError * error))failBlock;{
    if (self = [super init]) {
        self.recordType = type;
        self.successBlock = successBlock;
        self.failBlock = failBlock;
        self.isSaveAblum = isSaveAblum;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor blackColor];
    [self.view.layer addSublayer: self.previewLayer ];
    self.uiManage = [[CHCameraUIManage alloc]initWithController:self recordPhotoType:self.recordType isSaveAblum:self.isSaveAblum SuccessBlock:self.successBlock failBlock:self.failBlock];
    [self sessionConfiguration];

}



- (IBAction)startCameraBtn:(UIButton *)sender {
 
}

-(void)sessionConfiguration{
  

 
    
    if (self.recordType==CH_RECORD_PHOTO_TYPE_FILE_MOVIDE) {
        [self.recordFile startPreView:self.previewLayer];

        [self.uiManage setChRecord:self.recordFile];
  
    }else if(self.recordType == CH_RECORD_PHOTO_TYPE_DATA){
  
        [self.uiManage setChRecord:self.cameraManage];
    }else if(self.recordType == CH_RECORD_PHOTO_TYPE_PHOTO){
        [self.recordPhoto startPreView:self.previewLayer];

        [self.uiManage setChRecord:self.recordPhoto];
    }
  
  
    
    CHCameraUIView * cameraUIView =[[NSBundle mainBundle]loadNibNamed:NSStringFromClass([CHCameraUIView class]) owner:self options:nil].lastObject;
    cameraUIView.frame = CGRectMake(0, 0,self.view.frame.size.width, self.view.frame.size.height);

    [cameraUIView setUiManage:self.uiManage];
    [self.view addSubview:cameraUIView];

   
}

-(void)recordFileBlock{
  
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:NO];
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:NO];
}

#pragma mark --

-(CHRecordData *)cameraManage{
    if (!_cameraManage) {
        _cameraManage =  [[CHRecordData alloc]init];
    }
    return _cameraManage;
}

-(CHRecordFile *)recordFile{
    if(!_recordFile){
        _recordFile = [[CHRecordFile alloc]init];
    }
    return _recordFile;
}

-(CHRecordPhoto *)recordPhoto{
    if (!_recordPhoto) {
        _recordPhoto = [[CHRecordPhoto alloc]init];
    }
    return _recordPhoto;
}

/*
   添加预览
 */
-(AVCaptureVideoPreviewLayer *)previewLayer{
    if (!_previewLayer) {
//        _previewLayer = [[AVCaptureVideoPreviewLayer alloc]initWithSession:self.session];
        _previewLayer = [[AVCaptureVideoPreviewLayer alloc] init];
        _previewLayer.frame = self.view.bounds;
        _previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
     
    }
    return _previewLayer;
}
@end
